#generator  example1
def generator_function():
    for i in range(10):
        yield i

for item in generator_function():
    print(item)

#generator exmaple2
#generator version
def fibon(n):
    a = b = 1
    for i in range(n):
        yield a  #生出 一个返回值，但是，是可以迭代的。不过是通过生出产生，不会消耗大量内存
        a, b = b, a + b

for x in  fibon(100):
    print(x)

def fibon(n):
    a = b = 1
    result = []  
    for i in range(n):
        result.append(a)  #这样会消耗大量内存
        a , b = b, a + b
    return result

#generator example3
def generator_function():
    for i in range(3):
        yield i

gen = generator_function()
print(next(gen))
print(next(gen))
print(next(gen))
# print(next(gen))  #所有值已经被yield完了，不能再next()了

my_string = "Yasoob" 
# next(my_string) #错误，因为str对象是可迭代对象，但是还不是迭代器。

my_iter = iter(my_string) #通过iter 将可迭代对象转成迭代器
print(next(my_iter)) 